/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.MonAn;
import UTIL.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ChanhTin
 */
public class MonAnDAO {
     
    /**
     * @desc lay danh sach mon an
     * @return 
     */
    public static List<MonAn> layDanhSach() {
        List<MonAn> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select ma from MonAn ma";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;  
    }
    
    /**
     * @desc tim mon an theo ma mon an
     * @param maMonAn
     * @return 
     */
    public static MonAn layThongTinMonAn(int maMonAn) {
        MonAn ma = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            ma = (MonAn) session.get(MonAn.class, maMonAn);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ma;
    }
    
    /**
     * @desc them mon an
     * @param ma
     * @return 
     */
    public static boolean themMonAn(MonAn ma) throws HibernateException{
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (MonAnDAO.layThongTinMonAn(ma.getMaMonAn())!=null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.save(ma); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag;
    }
    
    /**
     * @desc cap nhat thong tin mon an
     * @param ma
     * @return 
     */
    public static boolean capNhatThongTinMonAn(MonAn ma) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (MonAnDAO.layThongTinMonAn(ma.getMaMonAn()) == null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.update(ma); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
           //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    } 
    
    /**
     * @desc xoa mon an
     * @param maMonAn
     * @return 
     */
    public static boolean xoaMonAn(int maMonAn) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        MonAn ma = MonAnDAO.layThongTinMonAn(maMonAn); 
        if(ma==null){ 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.delete(ma); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    }
}
